<?php

namespace app\model;

use think\Model;
use think\Request;
use think\Session;

class Base extends Model
{
    protected static function init()
    {
//        Self::afterInsert(function ($model) {
//            $base_name = basename(str_replace('\\', '/', get_called_class()), '.php');
//            $request = Request::instance();
//            $data = [];
//            $data["user_id"] = Session::has('user_id') ? Session::get('user_id') : '0';
//            $data["admin_id"] = Session::has('admin_id') ? Session::get('admin_id') : '0';
//            $data['type'] = 'after_insert';
//            $data['url'] = $request->url();
//            $data['model_name'] = $base_name;
//            $data["param"] = json_encode($request->param());
//            $data["ip"] = $request->ip();
//            $data["content"] = json_encode($model->getData());
//            $data["create_time"] = time();
//            $data["update_time"] = time();
//            $model->table('tp_system_log')->insert($data);
//        });


        self::afterUpdate(function ($model) {
            $base_name = basename(str_replace('\\', '/', get_called_class()), '.php');
            $request = Request::instance();
            $data = [];
            $data["user_id"] = Session::has('user_id') ? Session::get('user_id') : '0';
            $data["admin_id"] = Session::has('admin_id') ? Session::get('admin_id') : '0';
            $data['type'] = 'after_update';
            $data['url'] = $request->url();
            $data['model_name'] = $base_name;
            $data["param"] = json_encode($request->param());
            $data["ip"] = $request->ip();
            $data["content"] = json_encode($model->getData());
            $data["create_time"] = time();
            $data["update_time"] = time();
            $model->table('tp_system_log')->insert($data);
        });
    }

}